package day01;

import util.JdbcUtil;

import java.sql.*;

public class TestJDBC3 {

    public static void main(String[] args) throws Exception {





        // create();

        // insert();

        // update();

        // delete();

        // select();

        // selectById(3);

        // selectByName("abc");

        // selectByName("aaa' or '1'='1");
    }





    public static void create(){
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=JdbcUtil.getConnection();

            String sql=new StringBuffer()
                    .append("create table t_user( ")
                    .append("    id int primary key auto_increment, ")
                    .append("    name varchar(10) ")
                    .append(") ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.execute();


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(con,ps,null);
        }
    }

    public static void insert(){
        Connection con=null;
        PreparedStatement ps=null;
        try{

            con=JdbcUtil.getConnection();

            String sql=new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (name) ")
                    .append("values ")
                    .append("    (?) ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setString(1,"ddd");

            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(con,ps,null);
        }
    }

    public static void update(){
        Connection con=null;
        PreparedStatement ps=null;
        try{

            con=JdbcUtil.getConnection();

            String sql=new StringBuffer()
                    .append("update t_user ")
                    .append("set name=? ")
                    .append("where id=? ")
                    .toString();


            ps=con.prepareStatement(sql);

            ps.setString(1,"abc");
            ps.setInt(2,3);

            ps.executeUpdate();

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(con,ps,null);
        }
    }

    public static void delete(){
        Connection con=null;
        PreparedStatement ps=null;
        try{

            con=JdbcUtil.getConnection();

            String sql=new StringBuffer()
                    .append("delete from t_user ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setInt(1,2);

            ps.executeUpdate();


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(con,ps,null);
        }
    }

    public static void select(){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{

            con=JdbcUtil.getConnection();

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .toString();

            ps=con.prepareStatement(sql);

            rs=ps.executeQuery();

            while(rs.next()){
                // int id=rs.getInt(1);
                // String name=rs.getString(2);

                int id=rs.getInt("id");
                String name=rs.getString("name");

                System.out.println(id+"\t"+name);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(con,ps,rs);
        }
    }

    public static void selectById(int id){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{

            con=JdbcUtil.getConnection();

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setInt(1,id);

            rs=ps.executeQuery();

            if(rs.next()){
                // int id=rs.getInt(1);
                // String name=rs.getString(2);

                id=rs.getInt("id");
                String name=rs.getString("name");

                System.out.println(id+"\t"+name);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(con,ps,rs);
        }
    }


    public static void selectByName(String name){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{

            con=JdbcUtil.getConnection();

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where name=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setString(1,name);

            rs=ps.executeQuery();



            while(rs.next()){
                // int id=rs.getInt(1);
                // String name=rs.getString(2);

                int id=rs.getInt("id");
                name=rs.getString("name");

                System.out.println(id+"\t"+name);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(con,ps,rs);
        }
    }


}
